﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using DevTeam;
using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Siparisler
{
    public partial class MaliyetYeni : XForm
    {
        public MaliyetYeni()
        {
            InitializeComponent();
            TAM.MaliyetTableAdapter = new Datasets.MyDataTableAdapters.MaliyetTableAdapter();
        }

        public int MaliyetId;
        BindingSource bs;
        private void MaliyetYeni_Load(object sender, EventArgs e)
        {
            bs = new BindingSource(ds, "Maliyet");
            AddBsToList(bs);
            gleAtelye.Properties.Assign(RI(Rp.Atelye));
            gleBrand.Properties.Assign(RI(Rp.Brand));
            gleCurrency.Properties.Assign(RI(Rp.Currency));
            glePriceTerm.Properties.Assign(RI(Rp.PriceTerm));
            gleScenario.Properties.Assign(RI(Rp.Scenario));
            gleSezon.Properties.Assign(RI(Rp.Sezon));

            teEUR.Text = formatted(Session.Eur);
            teUSD.Text = formatted(Session.Usd);
            teGBP.Text = formatted(Session.Gbp);
            btnSave.ItemClick += BtnSave_ItemClick;
        }

        string formatted(double d)
        {
            return d.ToString();
            /*
            IFormatProvider iFormatProvider = System.Globalization.CultureInfo.InvariantCulture;
            //new System.Globalization.CultureInfo("es-ES");
            //string s2 = string.Format(iFormatProvider, "{0:#,##0.000}", val)
            //Note that the , and . are using a 'standard' en-US style, but .ToString() and string.Format() with a format provider does the right thing.

            return d.ToString("#.##0,000", iFormatProvider);
             */
        }

        private void BtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MyData.MaliyetRow r = ds.Maliyet.NewMaliyetRow();
            r.BrandId = intParse(gleBrand.EditValue);
            r.SezonId = intParse(gleSezon.EditValue);
            r.OrderRef = teOrderRef.Text;
            r.CurrencyId = intParse(gleCurrency.EditValue);
            r.PriceTermId = intParse(glePriceTerm.EditValue);
            r.Usd = (decimal)(double.Parse(teUSD.Text));
            r.Eur = (decimal)(double.Parse(teEUR.Text));
            r.Gbp = (decimal)(double.Parse(teGBP.Text));
            r.Notes = meNotes.Text;
            r.AtelyeId = intParse(gleAtelye.EditValue);
            r.Tarih = DateTime.Today;
            r.Versiyon = 1;
            r.ScenarioId = intParse(gleScenario.EditValue);
            r.UserId = WhoAmI;
            ds.Maliyet.AddMaliyetRow(r);
            
            Save();
            MaliyetId = ds.Maliyet[0].MaliyetId;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }
}